Skip to content

Assembly Language

Alt text

OpcodeOperandExplanation
LDM#nImmediate addressing. Load the number n to ACC
LDD<address>Direct addressing. Load the contents of the location at the given address to ACC
LDI<address>Indirect addressing. The address to be used is at the given address. Load the contents of this second address to ACC
LDX<address>Indexed addressing. Form the address from <address> + the contents of the index register. Copy the contents of this calculated address to ACC
LDRnImmediate addressing. Load the number n to IX
MOV<register>Move the contents of the accumulator to the given register (IX)
STO<address>Store the contents of ACC at the given address
ADD<address>Add the contents of the given address to the ACC
ADD#n/Bn/&nAdd the number n to the ACC
SUB<address>Subtract the contents of the given address from the ACC
SUB#n/Bn/&nSubtract the number n from the ACC
INC<register>Add 1 to the contents of the register (ACC or IX)
DEC<register>Subtract 1 from the contents of the register (ACC or IX)
JMP<address>Jump to the given address
CMP<address>Compare the contents of ACC with the contents of <address>
CMP#nCompare the contents of ACC with number n
CMI<address>Indirect addressing. The address to be used is at the given address. Compare the contents of ACC with the contents of this second address
JPE<address>Following a compare instruction, jump to <address> if the compare was True
JPN<address>Following a compare instruction, jump to <address> if the compare was False
INKey in a character and store its ASCII value in ACC
OUTOutput to the screen the character whose ASCII value is stored in ACC
ENDReturn control to the operating system

TIP

  • All questions will assume there is only one general purpose register available (Accumulator) ACC denotes Accumulator
  • IX denotes Index Register
  • <address> can be an absolute or symbolic address
  • # denotes a denary number, e.g. #123
  • B denotes a binary number, e.g. B01001010 & denotes a hexadecimal number, e.g. &4A

Assembly language and machine code

  • The only programming language that a CPU can use is machine code.
  • Every different type of computer/chip has its own set of machine code instructions.
  • A computer program stored in main memory is a series of machine code instructions that the CPU can automatically carry out during the fetch-execute cycle.
  • The first programming language to be developed was assembly language, this is closely related to machine code and uses mnemonics instead of binary.

Alt text

Stages of assembly

  • Before a program written in assembly language (source code) can be executed, it needs to be translated into machine code.
  • The translation is performed by a program called an assembler.
  • An assembler translates each assembly language instruction into a machine code instruction.
  • An assembler also checks the syntax of the assembly language program to ensure that only opcodes from the appropriate machine code instruction set are used.

Alt text

  • A two pass assembler produces an object program in machine code that can be stored, loaded then executed at a later stage.
  • This requires the use of another program called a loader.
  • Two pass assemblers need to scan the source program twice, so they can replace labels in the assembly program with memory addresses in the machine code program.

Pass 1

  • Read the assembly language program one line at a time.
  • Ignore anything not required, such as comments.
  • Allocate a memory address for the line of code.
  • Check the opcode is in the instruction set.
  • Add any new labels to the symbol table with the address, if known.
  • Place address of labelled instruction in the symbol table.

Pass 2

  • Read the assembly language program one line at a time.
  • Generate object code, including opcode and operand, from the symbol table generated in Pass 1.
  • Save or execute the program.

Assembly language and machine code

The only programming language that a CPU can use is .

[0/1]
machine code

Assembly language and machine code

The translates each assembly language instruction into a machine code instruction.

[0/1]

Assembly language instructions

Data movement instructions

OpcodeOperandExplanation
LDM#nImmediate addressing. Load the number n to ACC
LDD<address>Direct addressing. Load the contents of the location at the given address to ACC
LDI<address>Indirect addressing. The address to be used is at the given address. Load the contents of this second address to ACC
LDX<address>Indexed addressing. Form the address from <address> + the contents of the index register. Copy the contents of this calculated address to ACC
LDRnImmediate addressing. Load the number n to IX
MOV<register>Move the contents of the accumulator to the given register
STO<address>Store the contents of ACC at the given address

Input and output of data instructions

OpcodeOperandExplanation
INKey in a character and store its ASCII value in ACC
OUTOutput to the screen the character whose ASCII value is stored in ACC

TIP

  • No opcode is required as a single character is either input to the accumulator or output

Arithmetic operation instructions

OpcodeOperandExplanation
ADD<address>Add the contents of the given address to the ACC
ADD#n/Bn/&nAdd the number n to the ACC
SUB<address>Subtract the contents of the given address from the ACC
SUB#n/Bn/&nSubtract the number n from the ACC
INC<register>Add 1 to the contents of the register (ACC or IX)
DEC<register>Subtract 1 from the contents of the register (ACC or IX)

TIP

  • Answers to calculations are always stored in the accumulator

Unconditional and conditional instructions

OpcodeOperandExplanation
JMP<address>Jump to the given address
JPE<address>Following a compare instruction, jump to <address> if the compare was True
JPN<address>Following a compare instruction, jump to <address> if the compare was False
ENDReturn control to the operating system

TIP

  • Jump means change the PC to the address specified, so the next instruction to beexecuted is the one stored at the specified address, not the one stored at the next location in memory

Compare instructions

OpcodeOperandExplanation
CMP<address>Compare the contents of ACC with the contents of <address>
CMP#nCompare the contents of ACC with number n
CMI<address>Indirect addressing. The address to be used is at the given address. Compare the contents of ACC with the contents of this second address

TIP

  • The contents of the accumulator are always compared

Addressing modes

  • Assembly language and machine code programs use different addressing modes depending on the requirements of the program.

  • Absolute addressing – the contents of the memory location in the operand are used. For example, LDD 200.

  • Direct addressing – the contents of the memory location in the operand are used. For example, LDD 200.

  • Indirect addressing – the contents of the contents of the memory location in the operand are used. For example, LDI 200.

  • Indexed addressing – the contents of the memory location found by adding the contents of the index register (IR) to the address of the memory location in the operand are used. For example, LDX 200.

  • Immediate addressing – the value of the operand only is used. For example, LDM #200.

  • Relative addressing – the memory address used is the current memory address added to the operand. For example, JMR #5.

  • Symbolic addressing – only used in assembly language programming. A label is used instead of a value. For example, LDD MyStore.

Simple assembly language programs

  • A program written in assembly language will need many more instructions than a program written in a high-level language to perform the same task.

Alt text

Alt text

  • In a high-level language, adding a list of numbers together and storing the answer would typically be written using a loop.

Alt text

Alt text

Assembly language and machine code

State the contents of the accumulator after the following instructions have been executed. The memory location with address 200 contains 300, the memory location with address 300 contains 50.

  • LDM #200 (ACC: )
  • LDD 200 (ACC: )
  • LDI 200 (ACC: )
[0/1]

Assembly language and machine code

Write an assembly language instruction to:

  • compare the accumulator with 5:
  • jump to address 100 if the comparison is true:
[0/2]

Assembly language and machine code

What is the value of total in the end?

Label Opcode Operand
LDD number1
SUB number2
ADD number3
CMP #10
JPE nomore
ADD number4
nomore: STO total
END
number1: #30
number2: #40
number3: #20
number4: #50
total: #0
[0/3]